﻿
using SIE.SCADA.Common.Log;
using SIE.SCADA.UICommon.AutoUI;
using SIE.SCADA.ZemtDemo.Machines;
using SIE.SCADA.ZemtDemo.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SIE.SCADA.ZemtDemo.View
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class StationView : UserControl
    {
        private TorqueWrench torqueWrench;
        Thickness lblthickness = new Thickness();

        /// <summary>
        /// KJF 20210317 修改:工单显示框中的“工艺流程”改为“工序编码；”
        /// </summary>
        /// <param name="machine"></param>
        public StationView(IBaseMachine machine)
        {
            InitializeComponent();
            if(machine is  TorqueWrench viewModel)
            {
                torqueWrench = viewModel;
                var woInfo = torqueWrench.WoInfoViewModel;
                lblthickness.Right = 5;
                lblthickness.Left = 5;
                var woinfoView = new AutoView(woInfo, 1, Orientation.Vertical,
                    //nameof(woInfo.WO),
                    //nameof(woInfo.SN),
                    //nameof(woInfo.MBOM),

                    //nameof(woInfo.TechProcess),
                    //nameof(woInfo.JobstepVersion),

                    //nameof(woInfo.ProcessCode),
                    //nameof(woInfo.ProcessDesc),

                    //nameof(woInfo.Code),
                    //nameof(woInfo.PlanQuantity))
                    nameof(woInfo.Matid),
                    nameof(woInfo.Productid),
                    nameof(woInfo.RepairCode),
                    nameof(woInfo.SN),
                    nameof(woInfo.ProcessCode),
                    nameof(woInfo.ProcessDesc)
                    )

                { FontSize=16,Margin = lblthickness };

               // woinfoView.SetDataContext(nameof(woInfo.TechProcess),
               // woInfo.TechProcessSource,
               //  "Key", "Value"
               //  );
               // woinfoView.SetDataContext(nameof(woInfo.JobstepVersion),
               //woInfo.JobVersionSource,
               // "Key", "Value"
               // );

                sp_WoInfo.Content = woinfoView;
                this.DataContext = torqueWrench;
            }
        }

        private void Close_Utc(object sender, System.ComponentModel.HandledEventArgs e)
        {

        }

        private void LoginOut(object sender, RoutedEventArgs e)
        {

        }

        private void UploadClick(object sender, RoutedEventArgs e)
        {

        }

        private void BypassClick(object sender, RoutedEventArgs e)
        {
            //torqueWrench?.ByPass();
        }

        private void sp_WoInfo_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        //private void torqueWrench_IndexChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        if (torqueWrench != null && smart_AllStep.Items.Count > torqueWrench.GridIndex)
        //        {
        //            if (torqueWrench != null && smart_AllStep.Items.Count > torqueWrench.GridIndex && torqueWrench.GridIndex >= 0)
        //            {
        //                smart_AllStep.ScrollIntoView(smart_AllStep.Items[torqueWrench.GridIndex]);
        //                smart_AllStep.UpdateLayout();
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        System.Diagnostics.Debug.WriteLine($"刷新进度条失败！{ex.Message}");
        //    }
        //}

        

        /// <summary>
        /// 手动输入物料序列号和物料号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Enter_Press(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    var text = this.tbSN.Text;
                    if (string.IsNullOrEmpty(text)) return;
                    var res = torqueWrench?.RequestWoInfo(text);
                }
            }
            catch (Exception ex) {
                Logger.Error(ex.Message);
                Logger.Error(ex.StackTrace);
                MessageBox.Show(ex.Message);
            
            }
        }

        private void CallingRest(object sender, RoutedEventArgs e)
        {

        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                torqueWrench?.Clear();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message);
                Logger.Error(ex.StackTrace);
                MessageBox.Show(ex.Message);

            }
        }

        /// <summary>
        /// “工序重打”按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RetryProcessClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (torqueWrench == null) return;
                if (torqueWrench.RetryProcess())
                {

                    MessageBox.Show("开始重打当前工序！");

                }
                else
                {
                    MessageBox.Show("当前工序为空！");
                }
            }
            catch (Exception ex) {
                Logger.Error(ex.Message);
                Logger.Error(ex.StackTrace);
                MessageBox.Show(ex.Message);

            }
        }

        /// <summary>
        /// 工步“重打”按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RetryStepClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (torqueWrench == null) return;
                if (!torqueWrench.RetryStep(out var msg))
                {
                    MessageBox.Show(msg);
                }
                else
                {
                    MessageBox.Show("请扫码后开始重打");
                }
            }
            catch (Exception ex) {
                Logger.Error(ex.Message);
                Logger.Error(ex.StackTrace);
                MessageBox.Show(ex.Message);


            }
        }

        private void SpareBotton(object sender, RoutedEventArgs e)
        {

        }
    }
}
